//
//  UpdatePwdController.m
//  Estate
//
//  Created by heyong on 16/6/23.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "UpdatePwdController.h"
#import "QianDTableCell.h"
#import "ConfirmPwdCell.h"
@interface UpdatePwdController ()
@property(copy, nonatomic)NSString *oldPwd;
@property(copy, nonatomic)NSString *password;
@property(copy, nonatomic)NSString *conPwd;
@property (assign, nonatomic)BOOL isShow;

@end

@implementation UpdatePwdController
- (UITableView *)tableView{
    if(_tableView==nil){
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT)];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.bounces=NO;
        _tableView.showsVerticalScrollIndicator=NO;
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.isShow=YES;
    [self.view addSubview:self.tableView];
    self.title=@"修改密码";
    self.tableView.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
    self.tableView.backgroundColor=[UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
    [self.tableView registerNib:[UINib nibWithNibName:@"QianDTableCell" bundle:nil] forCellReuseIdentifier:@"acell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ConfirmPwdCell" bundle:nil] forCellReuseIdentifier:@"conCell"];
    self.password=@"";
    self.oldPwd=@"";
    self.conPwd=@"";
    
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"wancheng"] style:UIBarButtonItemStyleDone target:self action:@selector(clickSave)];
    
}


- (void)clickSave{
    NSLog(@"修改密码");
    if([self.oldPwd isEqualToString: @""]){
        [LSUtility showToastWithMessage:@"原密码为空" inView:self.view Andtime:1.];
    }else if ([self.password isEqualToString:@""]){
        [LSUtility showToastWithMessage:@"请输入新密码" inView:self.view Andtime:1.];
    }else if([self.conPwd isEqualToString:@""]){
        [LSUtility showToastWithMessage:@"请输入确认密码" inView:self.view Andtime:1.];
    }else if (![self.conPwd isEqualToString:self.password]){
        [LSUtility showToastWithMessage:@"两次密码输入不同" inView:self.view Andtime:1.];
    }else{
        NSMutableDictionary *pragmaDic=[NSMutableDictionary dictionary];
        [pragmaDic setObject:self.oldPwd forKey:@"password"];
        [pragmaDic setObject:self.password forKey:@"newPass"];
        [pragmaDic setObject:self.conPwd forKey:@"newAgainPass"];

        [LSHttpTool Post:URL_UPDATA_PWD parameters:pragmaDic success:^(id responseObject) {
            NSLog(@"%@",responseObject);
            NSString *msg=responseObject[@"msg"];
            if([responseObject[@"code"] isEqualToString:@"1"]){
                [LSUtility showToastWithMessage:msg inView:self.view Andtime:1];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }else{
                [MBProgressHUD showError:msg toView:self.view];
               
            }
       
        } failure:^(NSError *error) {
        }];
      
    }
    
}


#pragma mark --UITableViewDelagate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if(section==2){
        return 30;
    }else
        return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UILabel *footLel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 30)];
    footLel.text=@"\t\t密码由6-20位英文字母、数字或符号组成";
    footLel.font=[UIFont systemFontOfSize:13.];
    footLel.textColor=[UIColor colorWithRed:0.46 green:0.46 blue:0.47 alpha:1.00];
    return footLel;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WEAKSELF
    if(indexPath.section==0){
        QianDTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"acell" forIndexPath:indexPath];
        [cell configWithPlaceholder:@"请输入密码" andValue:weakSelf.oldPwd andLabel:@"原密码" andKeyType:UIKeyboardTypeASCIICapable isShpw:self.isShow andFontSize:16.];
        cell.textField.clearButtonMode = UITextFieldViewModeWhileEditing;

        cell.textValueChangedBlock= ^(NSString *valueStr){
            weakSelf.oldPwd = valueStr;
        };
        
        return cell;
        
    }else if (indexPath.section==1){
        QianDTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"acell" forIndexPath:indexPath];
        [cell configWithPlaceholder:@"请输入密码" andValue: weakSelf.password andLabel:@"新密码" andKeyType:UIKeyboardTypeASCIICapable isShpw:self.isShow andFontSize:16.];
        cell.textField.clearButtonMode = UITextFieldViewModeWhileEditing;

        cell.textValueChangedBlock=^(NSString *valueStr){
            weakSelf.password=valueStr;
        };
        return cell;
    }else{
        ConfirmPwdCell *conCell=[tableView dequeueReusableCellWithIdentifier:@"conCell" forIndexPath:indexPath];
        conCell.conPwd.clearButtonMode = UITextFieldViewModeWhileEditing;
        conCell.textValueChangedBlock=^(NSString *valueStr){
            weakSelf.conPwd=valueStr;
        };
        conCell.conPwd.secureTextEntry=self.isShow;
        //解决strongly in this block is likely to lead to a retain cycle 警告
//        __block ConfirmPwdCell *cellBlock = conCell;
        conCell.sValueChangeBlock=^(UISwitch *isShow){
            if(isShow.on){
                self.isShow=NO;
            }else{
                self.isShow=YES;
            }
            [weakSelf.tableView reloadData];
        };
        
        return conCell;
        
    }
}


/*
 *  此方法让cell横线置顶
 */
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
}
@end
